<template>
  <div class="pf-menu pf-bgwhite pf-p-10">
    <el-button icon="Refresh" @click="select" />
    <pf-table
      :data="tableData"
      :config="tableConfig"
      :row-key="'menuId'"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="操作" fixed="right" width="80">
        <template #default="scope">
          <el-tooltip effect="light" placement="left" trigger="hover">
            <component :is="'Menu'" class="el-button--text" style="width: 18px"></component>
            <template #content>
              <div class="pf-flex pf-flex-column pf-flex-0">
                <el-button @click="handleClick(scope.row, 'edit')" link type="primary" size="mini"
                  >编辑</el-button
                >
                <el-button @click="handleClick(scope.row, 'del')" link type="primary" size="mini"
                  >删除</el-button
                >
              </div>
            </template>
          </el-tooltip>
        </template>
      </el-table-column>
    </pf-table>
    <el-dialog :width="'80%'" v-model="menuDialogVis" title="菜单信息">
      <pf-form ref="menuFormRef" :model="formInfo" :config="formConfig"></pf-form>
      <template #footer>
        <el-button type="primary" @click="confirmCreate">确认</el-button>
        <el-button @click="menuDialogVis = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, Ref, onMounted } from 'vue';
import { systemApi } from '@/constants/api/system-api';
import { useNotice } from '@/components/element-plus/notice';
import {
  useData,
  useDict,
  emptyForm,
  FormModel,
  useHttpClient,
  emptyTable,
  TableModel,
  SysDict,
  ResponseCodeEnum,
  SysMenu,
  useMenu,
  FormFieldModel,
  Options
} from 'pf-component';
import { FormNameEnum } from '@/constants/enum/form-name.enum';
import { TableNameEnum } from '@/constants/enum/table-name.enum';

export default defineComponent({
  name: 'Menu',
  setup() {
    const { message } = useNotice();
    const tableConfig: Ref<TableModel> = ref(emptyTable);
    const tableData: Ref<SysMenu[]> = ref([]);

    const menuDialogVis: Ref<boolean> = ref(false);
    const formConfig: Ref<FormModel> = ref(emptyForm);
    const formInfo: Ref<SysMenu> = ref(null as any);
    const menuFormRef = ref(null);
    onMounted(async () => {
      const formPromise = useData().loadForm([
        {
          name: FormNameEnum.sysMenuForm,
          config: formConfig,
          info: formInfo
        }
      ]);
      const tablePromise = useData().loadTable([
        { name: TableNameEnum.sysMenu, config: tableConfig }
      ]);
      await Promise.all([formPromise, tablePromise]).then((res: boolean[]) => {
        tableConfig.value.rowKey = 'menuId';
        select();
      });
    });

    const select = () => {
      useMenu()
        .list()
        .then(res => {
          tableData.value = res;
        });
    };
    const create = () => {
      formInfo.value = useData().toFormValue(formConfig.value as any);
      menuDialogVis.value = true;
    };
    const confirmCreate = async () => {
      console.log('valid');
      const valid = await (menuFormRef.value as any).formRef.validate();
      console.log(valid);
      if (valid) {
        useHttpClient()
          .general(systemApi.menuApi.upd, undefined, formInfo.value)
          .then(res => {
            if (res.code === ResponseCodeEnum.SUCCESS) {
              menuDialogVis.value = false;
              select();
            } else {
              message.error(res.message);
            }
          });
      }
    };
    const del = () => {
      // systemApi.dictApi.delete
      message.error('待完善');
    };
    const handleClick = (data: SysMenu, cmd: string) => {
      switch (cmd) {
        case 'edit':
          formInfo.value = data;
          menuDialogVis.value = true;
          formConfig.value.setOptions('menuSupMenuId', transTreeProps(tableData.value));
          break;
        case 'del':
          del();
          break;
        default:
          message.error('未定义的操作！');
      }
    };
    const transTreeProps = (menus: SysMenu[]): Options[] => {
      const datas = menus || [];
      if (datas.length <= 0) {
        return [];
      }
      return datas.map(data => {
        return {
          key: data.menuId,
          value: data.menuName,
          leaf: !data.children || data.children.length === 0,
          children: transTreeProps(data.children)
        };
      });
    };
    return {
      select,
      formInfo,
      formConfig,
      menuFormRef,
      menuDialogVis,
      tableConfig,
      tableData,
      handleClick,
      create,
      confirmCreate,
      del
    };
  }
});
</script>

<style scoped lang="scss">
.#{$prefix} {
  &-menu {
  }
}
</style>
